<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{totalPrice}}</text>
		</view>

		<view class="pay-type-list">

			<view class="type-item b-b" @click="changePayType(1)" v-if="platPay">
				<text class="icon yticon icon-weixinzhifu"></text>
				<view class="con">
					<text class="tit">平台支付</text>
					<text>微信支付、支付宝支付</text>
				</view>
				<label class="radio">
					<radio value="" color="#FF6146" :checked='payType == 1' />
					</radio>
				</label>
			</view>
			<view class="type-item b-b" @click="changePayType(2)" style="display: none;">
				<text class="icon yticon icon-alipay"></text>
				<view class="con">
					<text class="tit">支付宝支付</text>
				</view>
				<label class="radio">
					<radio value="" color="#FF6146" :checked='payType == 2' />
					</radio>
				</label>
			</view>
			<view class="type-item" @click="changePayType(3)" style="display: none;">
				<text class="icon yticon icon-erjiye-yucunkuan"></text>
				<view class="con">
					<text class="tit">预存款支付</text>
					<text>可用余额 ¥198.5</text>
				</view>
				<label class="radio">
					<radio value="" color="#FF6146" :checked='payType == 3' />
					</radio>
				</label>
			</view>
			<view class="type-item" @click="changePayType(4)" v-if="redPacketPay">
				<text class="icon" style="font-size: 52upx;">&#xe60d;</text>
				<view class="con">
					<text class="tit">微信红包支付</text>
					<text>需要添加店主微信</text>
				</view>
				<label class="radio">
					<radio value="" color="#FF6146" :checked='payType == 4' />
					</radio>
				</label>
			</view>
		</view>

		<uni-section title="红包支付操作步骤" type="line" v-if="payType==4"></uni-section>
		<view class="example-body" v-if="payType==4">
			<uni-steps :options="list2" active-color="#007AFF" :img="redImg" :url="redUrl" :active="active" direction="column" />
		</view>
		<text class="mix-btn" @click="confirm">确认支付</text>
		<!-- #ifndef H5 -->
		<button v-if="status !== 2" type="primary" @click="pay" style="display: none;">支付</button>
		<!-- #endif -->
		<!-- #ifdef H5 -->
		<button v-if="!codeUrl && status !== 2" type="primary" @click="pay" style="display: none;">获取支付二维码</button>
		<template v-if="codeUrl && status !== 2" style="display: none;">
			<image :src="'http://qr.liantu.com/api.php?text=' + codeUrl" mode="widthFix"></image>
			<button type="primary" @click="orderQuery">我已完成支付</button>
		</template>
		<!-- #endif -->
	</view>
</template>

<script>
	import myCloud from '@/common/util/cloud.js';
	import myConst from '@/common/util/const.js';
	import util from '@/common/util/util.js';
	import uniSteps from '@/components/uni-ui/uni-steps/uni-steps.vue'
	import uniSection from '@/components/uni-ui/uni-section/uni-section.vue'
	import { getData,getDataById,editData } from '@/common/api/order.js'
	export default {
		components: {
			uniSteps,
			uniSection
		},
		data() {
			return {
				platPay:false,
				redPacketPay:false,
				payType: 1,
				orderInfo: {},
				orderId: '',
				state: 0,
				status: 0,
				totalPrice: 10000,
				active: 3,
				outTradeNo:'',
				codeUrl: '',
				redImg:'',
				redUrl:'',
				list2: [{
					title: '步骤一',
					desc: '预览图片，添加微信好友'
				}, {
					title: '步骤二',
					desc: '发送右侧链接'
				}, {
					title: '步骤三',
					desc: '发送微信红包'
				}, {
					title: '步骤四',
					desc: '交易完成'
				}]
			};
		},
		computed: {

		},
		onLoad(options) {
			this.orderId = options.orderId;
			this.state = options.state;
			
		},
		onShow() {
			this.getData();
			this.getSettingData();
		},

		methods: {
			async getData() {
				const req = {
					id: this.orderId
				}
				const res = await getDataById(req)
				if (res.length > 0) {
					this.totalPrice = res[0].totalPrice;
					this.outTradeNo = res[0].outTradeNo;
				}
			},
			async getSettingData() {
				const res = await getData({})
				if (res.length > 0) {
					if(res[0].data.redPacketPay=='开启'){
						this.redPacketPay = true
						this.redImg = res[0].data.adminQrCodeUrl
						this.redUrl = this.orderId
					}
					if(res[0].data.wechatPay=='开启'){
						this.platPay = true
					}
				}
			},
			//选择支付方式
			changePayType(type) {
				this.payType = type;
			},
			//确认支付
			confirm: async function() {
				const data = {
					state: parseInt(this.state),
					id: this.orderId
				}
				const res = await editData(data)
				uni.redirectTo({
					url: '/pages/pay/pay-success'
				})
			},
			// #ifdef H5
			pay() {
				const providerList = ['wxpay','alipay']
				let selectedProvider = ''
				new Promise((resolve, reject) => {
					uni.showActionSheet({
						itemList: providerList,
						success(res) {
							resolve(providerList[res.tapIndex])
						},
						fail() {
							reject(new Error('取消支付'))
						}
					})
				}).then((provider) => {
					selectedProvider = provider
					return uniCloud.callFunction({
						name: 'b_pay',
						data: {
							provider,
							outTradeNo: this.outTradeNo
						}
					})
				}).then((res) => {
					console.log(res);
					this.codeUrl = res.result.orderInfo.codeUrl
				}).catch((err) => {
					uni.showModal({
						content: err.message || '获取支付二维码失败',
						showCancel: false
					})
				})
			},
			orderQuery() {
				uniCloud.callFunction({
					name: 'b_pay_query',
					data: {
						outTradeNo: this.outTradeNo
					}
				}).then((res) => {
					if (res.result.orderPaid) {
						this.status = 2
						uni.showModal({
							content: '订单已支付',
							showCancel: false
						})
					} else {
						uni.showModal({
							content: '订单未支付',
							showCancel: false
						})
					}
				}).catch((e) => {
					uni.showModal({
						content: '查询订单状态失败，请稍后再试',
						showCancel: false
					})
				})
			},
			// #endif
			// #ifndef H5
			pay() {
				let selectedProvider = ''
				new Promise((resolve, reject) => {
					uni.getProvider({
						service: 'payment',
						success(res) {
							resolve(res.provider)
						},
						fail() {
							reject(new Error('获取支付方式失败'))
						}
					})
				}).then((providerList) => {
					return new Promise((resolve, reject) => {
						uni.showActionSheet({
							itemList: providerList,
							success(res) {
								resolve(providerList[res.tapIndex])
							},
							fail() {
								reject(new Error('取消支付'))
							}
						})
					})

				}).then((provider) => {
					selectedProvider = provider
					return uniCloud.callFunction({
						name: 'b_pay',
						data: {
							provider,
							outTradeNo: this.outTradeNo
						}
					})
				}).then((res) => {
					console.log(res);
					if (res.result.orderInfo) {
						return new Promise((resolve, reject) => {
							uni.requestPayment({
								// #ifdef APP-PLUS
								provider: selectedProvider,
								// #endif
								// #ifdef MP-WEIXIN
								...res.result.orderInfo,
								// #endif
								// #ifdef APP-PLUS || MP-ALIPAY
								orderInfo: res.result.orderInfo,
								// #endif
								complete() {
									resolve(res.result.outTradeNo)
								}
							})
						})
					} else {
						throw new Error(res.result.msg)
					}
				}).then((outTradeNo) => {
					return uniCloud.callFunction({
						name: 'b_pay_query',
						data: {
							outTradeNo
						}
					})
				}).then((res) => {
					if (res.result.orderPaid) {
						this.status = 2
						uni.showModal({
							content: '订单已支付',
							showCancel: false
						})
					} else {
						uni.showModal({
							content: '订单未支付',
							showCancel: false
						})
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '支付失败',
						showCancel: false
					})
				})

			}

			// #endif
		}
	}
</script>

<style lang='scss'>
	.icon {
		font-size: 25px;
		font-family: iconfont;
		color: #ff4444;
	}

	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			&:before {
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}

		.icon-weixinzhifu {
			color: #36cb59;
		}

		.icon-alipay {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}

	.example-body {
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: center;
		padding: 0;
		font-size: 14px;
		background-color: #ffffff;
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 15px;
		flex-direction: row;
	}
</style>
